package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.GraduateProjectStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
//声明数据库访问类
@Repository("graduateProjectStatusDao")
public class GraduateProjectStatusDao {
    //自动注入
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(GraduateProjectStatus graduateProjectStatus) {
        String sql = "INSERT INTO GraduateProjectStatus (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, graduateProjectStatus.getNo(), graduateProjectStatus.getDescription(),graduateProjectStatus.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }
    //根据id查找
    public GraduateProjectStatus find(int id){
        String sql = "SELECT * FROM GraduateProjectStatus WHERE id = ?";
        List<GraduateProjectStatus> graduateProjectStatuses = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(GraduateProjectStatus.class));
        if(!graduateProjectStatuses.isEmpty()){
            return graduateProjectStatuses.get(0);
        } else
            return null;
    }
    public List<GraduateProjectStatus> findAll(){
        String sql = "SELECT * FROM GraduateProjectStatus";

        List<GraduateProjectStatus> graduateProjectStatuses =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(GraduateProjectStatus.class){

        });
        return graduateProjectStatuses;
    }
    public boolean update(GraduateProjectStatus graduateProjectStatus){
        String sql = "UPDATE GraduateProjectStatus SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,graduateProjectStatus.getNo(),graduateProjectStatus.getDescription(),graduateProjectStatus.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM GraduateProjectStatus WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}